﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
    public class SqlAccess
    {
        private static string ConnectionString = @"Data Source=HUYNH\PASSTION;Initial Catalog=QLRapPhimDB;Integrated Security=True";
    
    //Lay du lieu tu database theo cau lênh sql
        public static DataTable ExecuteQuery(string sql)
        {
            //Tao ket noi cn den csdl
            SqlConnection cn = new SqlConnection(ConnectionString);
            //Open
            cn.Open();

            //Tao doi tuong thuc hien len ket noi
            SqlCommand cmd = new SqlCommand();
            //Gan ket noi cn cho doi tuong cmd
            cmd.Connection = cn;
            //Gan chuoi lay du lieu cho doi tuong cmd
            cmd.CommandText = sql;
            //Tao doi tuong van chuyen du lieu dt voi cmd da tao
            SqlDataAdapter dt = new SqlDataAdapter(cmd);
            DataTable kq = new DataTable();
            //Tra ve 1 bag chua csdl
            dt.Fill(kq);
            return kq;
        }

        //Ham de them xoa cap nhat du lieu
        public static string ExecuteNonQuery(string sql)
        {
                try
                {
                    //Tao ket noi den co so du lieu
                    SqlConnection cn = new SqlConnection(ConnectionString);
                    //Mo ket noi
                    cn.Open();
                    //Tao doi tuong thuc hien len ket noi
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cn;
                    //Gan chuoi ket noi du lieu cho cmd
                    cmd.CommandText = sql;
                    //Thuwc hien cac thao tac tren du lieu
                    cmd.ExecuteNonQuery();
                    //dong ket noi
                    cn.Close();
                    //Tra ve chuoi khi thuc hien thanh cong
                    return "True";
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
        }
    }
}
